using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using InfoJetSoft.Service;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Manager;
using InfoJetSoft.Service.Worker;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service
{
	public class InfoJetService
	{
		public static string BuildVersion
		{
			get
			{
				return "2012.12.24"; //BuildVersion
			}
		}

		public static string ServiceAction = "xdoc.aspx";

		public static void License( string licenseKey )
		{
			XSNCache.license( licenseKey );
		}

		public static string Register( HttpContext httpContext, byte[] xsnContent )
		{
			return XSNCache.register( httpContext, new InfoJetContext(), null , xsnContent , "" );
		}

		public static string Register( HttpContext httpContext, byte[] xsnContent , string webContext )
		{
            return XSNCache.register(httpContext, new InfoJetContext(), null, xsnContent, webContext);
		}
	
		public static bool Register( HttpContext httpContext, string publishUrl , byte[] xsnContent , string webContext )
		{
            string url = XSNCache.register(httpContext, new InfoJetContext(), publishUrl, xsnContent, webContext);
			if( url == null )
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		public static string Register( HttpContext httpContext, InfoJetContext jetContext, byte[] xsnContent )
		{
			return XSNCache.register( httpContext, jetContext, null , xsnContent , "" );
		}

		public static string Register( HttpContext httpContext, InfoJetContext jetContext, byte[] xsnContent , string webContext )
		{
			return XSNCache.register( httpContext, jetContext, null , xsnContent , webContext );
		}

		public static bool Register( HttpContext httpContext, InfoJetContext jetContext, string publishUrl , byte[] xsnContent , string webContext )
		{
			string url = XSNCache.register( httpContext, jetContext, publishUrl , xsnContent , webContext );
			if( url == null )
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		public static bool RegisterWithMD5( HttpContext httpContext, string publishUrl , byte[] xsnContent , string webContext , string md5 )
		{
            //CID 0618 //2012.12.24 //Begin
			string url = XSNCache.registerWithMD5( httpContext, new InfoJetContext(), publishUrl , xsnContent , webContext , md5 );
            //CID 0618 //2012.12.24 //End
			if( url == null )
			{
				return false;
			}
			else
			{
				return true;
			}
		}

        //CID 0618 //2011.05.29 //Begin
        public static bool RegisterWithMD5(HttpContext httpContext, InfoJetContext jetContext, string publishUrl, byte[] xsnContent, string webContext, string md5)
        {
            //CID 0618 //2012.12.24 //Begin
            string url = XSNCache.registerWithMD5(httpContext, jetContext, publishUrl, xsnContent, webContext, md5);
            //CID 0618 //2012.12.24 //End
            if (url == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //CID 0618 //2011.05.29 //End

		public static string GetMD5( string publishUrl )
		{
			return XSNCache.getXSNMD5( publishUrl );
		}
	
		public static string[] List()
		{
			return XSNCache.list();
		}
	
		public static void Remove( string publishUrl )
		{
			XSNCache.remove( publishUrl );
		}
	
		public static bool ContainsXSN( string publishUrl )
		{
			return XSNCache.containsXSN( publishUrl );
		}

		public static Manifest GetManifest( string publishUrl )
		{
			return XSNCache.getManifest( publishUrl );
        }
	
		public static InfoJetForm ReloadForm( HttpContext httpContext )
		{
			InfoJetContext jetContext = new InfoJetContext();
			return ReloadForm( httpContext, jetContext );
		}

		public static InfoJetForm ReloadForm( HttpContext httpContext , string userRole )
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			return ReloadForm( httpContext, jetContext );
		}

		public static InfoJetForm ReloadForm( HttpContext httpContext , InfoJetContext jetContext )
		{
			Hashtable paramTable = ParseParamTable( httpContext.Request );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_COMMAND , JetConstant.CMD_BUILD_BY_PARAMETERS );
			InfoJetForm form = FormManager.serviceForm( httpContext, jetContext, paramTable );
			return form;
		}

		public static InfoJetForm UpdateForm( HttpContext httpContext )
		{
			InfoJetContext jetContext = new InfoJetContext();
			return UpdateForm( httpContext, jetContext );
		}
	 
		public static InfoJetForm UpdateForm( HttpContext httpContext , string userRole ){
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			return UpdateForm( httpContext, jetContext );
		}

		public static InfoJetForm UpdateForm( HttpContext httpContext , InfoJetContext jetContext )
		{
			Hashtable paramTable = ParseParamTable( httpContext.Request );
			InfoJetForm form = FormManager.serviceForm( httpContext, jetContext, paramTable );
			return form;
		}

		public static InfoJetForm BuildFormByTemplate( HttpContext httpContext, string publishUrl )
		{
			InfoJetContext jetContext =new InfoJetContext();
			return BuildFormByTemplate( httpContext, jetContext, publishUrl );
		}
	
		public static InfoJetForm BuildFormByTemplate( HttpContext httpContext, string publishUrl , string userRole )
		{
			InfoJetContext jetContext =new InfoJetContext();
			jetContext.UserRole = userRole;
			return BuildFormByTemplate( httpContext, jetContext, publishUrl );
		}

		public static InfoJetForm BuildFormByTemplate( HttpContext httpContext, string publishUrl , string userRole, string viewName )
		{
			InfoJetContext jetContext =new InfoJetContext();
			jetContext.UserRole = userRole;
			jetContext.ViewName = viewName;
			return BuildFormByTemplate( httpContext, jetContext, publishUrl );
		}

		public static InfoJetForm BuildFormByTemplate( HttpContext httpContext, string publishUrl , InfoJetContext jetContext )
		{
			return BuildFormByTemplate( httpContext, jetContext, publishUrl );
		}

		public static InfoJetForm BuildFormByTemplate( HttpContext httpContext, InfoJetContext jetContext, string publishUrl )
		{
			Hashtable paramTable = new Hashtable();
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_XSN_URI , publishUrl );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_COMMAND , JetConstant.CMD_BUILD_BY_TEMPLATE );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_ACTION , ServiceAction );
			InfoJetForm form = FormManager.serviceForm( httpContext, jetContext, paramTable );
			return form;
		}


		public static InfoJetForm BuildFormByXML( HttpContext httpContext, string publishUrl , string xml )
		{
			InfoJetContext jetContext = new InfoJetContext();
			return BuildFormByXML( httpContext, jetContext, publishUrl, xml );
		}
	
		public static InfoJetForm BuildFormByXML( HttpContext httpContext, string publishUrl , string xml , string userRole )
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			return BuildFormByXML( httpContext, jetContext, publishUrl, xml );
		}

		public static InfoJetForm BuildFormByXML( HttpContext httpContext, string publishUrl , string xml , string userRole, string viewName )
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			jetContext.ViewName = viewName;
			return BuildFormByXML( httpContext, jetContext, publishUrl, xml );
		}

		public static InfoJetForm BuildFormByXML( HttpContext httpContext, string publishUrl , string xml , InfoJetContext jetContext )
		{
			return BuildFormByXML( httpContext, jetContext, publishUrl, xml );
		}

		public static InfoJetForm BuildFormByXML( HttpContext httpContext, InfoJetContext jetContext, string publishUrl , string xml )
		{
			Hashtable paramTable = new Hashtable();
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_XSN_URI , publishUrl );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_COMMAND , JetConstant.CMD_BUILD_BY_XML );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_XML , xml );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_ACTION , ServiceAction );
			InfoJetForm form = FormManager.serviceForm( httpContext, jetContext, paramTable );
			return form;
		}

		public static InfoJetForm BuildFormByMerger( HttpContext httpContext, string publishUrl , string[] xmls ) 
		{
			InfoJetContext jetContext = new InfoJetContext();
			return BuildFormByMerger( httpContext, jetContext, publishUrl, xmls );
		}

		public static InfoJetForm BuildFormByMerger( HttpContext httpContext, string publishUrl , string[] xmls , string userRole ) 
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			return BuildFormByMerger( httpContext, jetContext, publishUrl, xmls );
		}

		public static InfoJetForm BuildFormByMerger( HttpContext httpContext, string publishUrl , string[] xmls , string userRole, string viewName ) 
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			jetContext.ViewName = viewName;
			return BuildFormByMerger( httpContext, jetContext, publishUrl, xmls );
		}

		public static InfoJetForm BuildFormByMerger( HttpContext httpContext, string publishUrl , string[] xmls , InfoJetContext jetContext ) 
		{
			return BuildFormByMerger( httpContext, jetContext, publishUrl, xmls );
		}

		public static InfoJetForm BuildFormByMerger( HttpContext httpContext, InfoJetContext jetContext, string publishUrl , string[] xmls ) 
		{
			Hashtable paramTable =new Hashtable();
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_XSN_URI , publishUrl );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_COMMAND , JetConstant.CMD_BUILD_BY_MERGER );
			for( int i = 0; i < xmls.Length; i ++ )
			{
				HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_MERGE_XML_ + i, xmls[ i ] );
			}
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_ACTION , ServiceAction );
			InfoJetForm form = FormManager.serviceForm( httpContext, jetContext, paramTable );
			return form;
		}

		public static InfoJetForm BuildFormByTemplateMerger( HttpContext httpContext, string publishUrl , string paramXml ) 
		{
			InfoJetContext jetContext = new InfoJetContext();
			return BuildFormByTemplateMerger( httpContext, jetContext, publishUrl, paramXml );
		}

		public static InfoJetForm BuildFormByTemplateMerger( HttpContext httpContext, string publishUrl , string paramXml , string userRole ) 
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			return BuildFormByTemplateMerger( httpContext, jetContext, publishUrl, paramXml );
		}

		public static InfoJetForm BuildFormByTemplateMerger( HttpContext httpContext, string publishUrl , string paramXml , string userRole, string viewName ) 
		{
			InfoJetContext jetContext = new InfoJetContext();
			jetContext.UserRole = userRole;
			jetContext.ViewName = viewName;
			return BuildFormByTemplateMerger( httpContext, jetContext, publishUrl, paramXml );
		}

		public static InfoJetForm BuildFormByTemplateMerger( HttpContext httpContext, string publishUrl , string paramXml , InfoJetContext jetContext ) 
		{
			return BuildFormByTemplateMerger( httpContext, jetContext, publishUrl, paramXml );
		}

		public static InfoJetForm BuildFormByTemplateMerger( HttpContext httpContext, InfoJetContext jetContext, string publishUrl , string paramXml ) 
		{
			Hashtable paramTable =new Hashtable();
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_XSN_URI , publishUrl );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_COMMAND , JetConstant.CMD_BUILD_BY_TEMPLATEMERGER );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_MERGE_XML_ + "1", paramXml );
			HashtableUtil.addSafety( paramTable, JetConstant.XDOC_PARAM_ACTION , ServiceAction );
			InfoJetForm form = FormManager.serviceForm( httpContext, jetContext, paramTable );
			return form;
		}

		private static Hashtable ParseParamTable( HttpRequest request )
		{
			Hashtable paramTable = new Hashtable();
			NameValueCollection reqParams = request.Params;
			for( int i = 0 ; i < reqParams.Count ; i ++ )
			{
				if( reqParams.Keys[ i ] != null && reqParams.Keys[ i ].Length > 0 )
				{
					if( reqParams[ i ] == null )
					{
						paramTable[ reqParams.Keys[ i ] ] = "";
					}
					else
					{
						paramTable[ reqParams.Keys[ i ] ] = reqParams[ i ];
					}
				}
			}
			return paramTable;
		}

		//CID 0033 //2008.06.25 //Begin
		public static void ClearFormCache( HttpContext httpContext )
		{
			ClearSessionWorker.clearFormSession( httpContext );
		}
		//CID 0033 //2008.06.25 //End

		//CID 0033 //2008.06.25 //Begin
		public static void ClearUserCache( HttpContext httpContext )
		{
			ClearSessionWorker.clearUserSession( httpContext );
		}
		//CID 0033 //2008.06.25 //End

        //CID 0352 //2009.07.05 //Begin
        [Obsolete("Please backup the form xml in postback if required.")]
		public static string RecoverNonDirtyFormXml( HttpContext httpContext )
		{
			InfoJetContext jetContext = new InfoJetContext();
			Hashtable paramTable = ParseParamTable( httpContext.Request );
			return FormManager.BuildNonDirtyFormXml( httpContext, jetContext, paramTable );
		}
		//CID 0352 //2009.07.05 //End

        //CID 1001 //2011.08.02 //Begin
        public static InfoJetForm RecoverForm(HttpContext httpContext, InfoJetContext jetContext)
        {
            Hashtable paramTable = ParseParamTable(httpContext.Request);
            string xml = FormManager.BuildNonDirtyFormXml(httpContext, jetContext, paramTable);

            string publishUrl = paramTable[JetConstant.XDOC_PARAM_XSN_URI] as string;
            InfoJetForm oldForm = InfoJetService.BuildFormByXML(httpContext, jetContext, publishUrl, xml);

            paramTable.Remove(JetConstant.XDOC_PARAM_FORM_ID);
            paramTable[JetConstant.XDOC_PARAM_FORM_ID] = oldForm.InnerForm.Id;
            InfoJetForm form = FormManager.serviceForm(httpContext, jetContext, paramTable);
            return form;
        } 
        //CID 1001 //2011.08.02 //End

		//CID 0363 //2009.07.10 //Begin
		public static bool IsFormSessionDataLost( HttpContext httpContext )
		{
			return FormSession.isFormSessionDataLost( httpContext );
		}
		//CID 0363 //2009.07.10 //End
	}
}
